package com.zHan.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zHan.sky.common.contant.MessageConstant;
import com.zHan.sky.common.exception.DeletionNotAllowedException;
import com.zHan.sky.dto.DishDto;
import com.zHan.sky.entity.Dish;
import com.zHan.sky.entity.DishFlavor;
import com.zHan.sky.mapper.DishMapper;
import com.zHan.sky.service.DishFlavorService;
import com.zHan.sky.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存口味数据
     * @param dishDto
     */
    @Override
    @Transactional  //添加事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本信息到dish表
        this.save(dishDto);

        //获取dishId
        Long dishId = dishDto.getId();

        //给flavors赋值dishId
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach((item)->{
            item.setDishId(dishId);
        });

        //保存口味到dish_flavor表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询菜品和对应的口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品的基本信息，对应dish表
        Dish dish = this.getById(id);

        //创建dishDto对象
        DishDto dishDto=new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品的口味，对应dish_flavor表
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    /**
     * 修改菜品和对应的口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品的基本信息，对应dish表
        this.updateById(dishDto);

        //获取dishId
        Long dishId = dishDto.getId();

        //清除菜品的口味操作，对应dish_flavor表，remove
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        dishFlavorService.remove(queryWrapper);

        //插入菜品的口味，对应dish_flavor表，insert
        //给flavors赋值dishId
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach((item)->{
            item.setDishId(dishId);
        });

        //保存口味到dish_flavor表
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 删除菜品和对应的口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        //查询菜品信息，确定是否可以删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        dishLambdaQueryWrapper.in(Dish::getId,ids);

        //不能删除，抛出业务异常
        if (this.count(dishLambdaQueryWrapper)>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //可以删除，先删除dish表中的信息
        this.removeByIds(ids);

        //再删除dish_flavor表中的数据
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper =new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }

    /**
     * 得到各个状态的菜品数量
     * @param status
     * @return
     */
    @Override
    public Integer getStatusCount(Integer status) {
        return lambdaQuery()
                .eq(status != null, Dish::getStatus, status).count();
    }


}
